package com.znh.kuiklyuniversity.compose.widgets

import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.tencent.kuikly.compose.coil3.rememberAsyncImagePainter
import com.tencent.kuikly.compose.foundation.Image
import com.tencent.kuikly.compose.foundation.layout.height
import com.tencent.kuikly.compose.foundation.layout.width
import com.tencent.kuikly.compose.foundation.shape.RoundedCornerShape
import com.tencent.kuikly.compose.material3.Tab
import com.tencent.kuikly.compose.material3.TabRow
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.draw.clip
import com.tencent.kuikly.compose.ui.graphics.Color
import com.tencent.kuikly.compose.ui.layout.ContentScale
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.compose.ui.unit.sp
import com.tencent.kuikly.core.base.attr.ImageUri
import com.tencent.kuikly.core.log.KLog
import com.znh.kuiklyuniversity.backgroundColor
import com.znh.kuiklyuniversity.compose.entrys.TabsMenu
import com.znh.kuiklyuniversity.compose.selfenum.AppColor

@Composable
fun BottonLayout(menuList: List<TabsMenu>,controller:BottonController) {
    var selecctTab: Int by remember { controller.getCut() }
    KLog.i("znh","tabs defalut=="+"     "+selecctTab)
    TabRow(
        selectedTabIndex = selecctTab,
        modifier = Modifier
            .backgroundColor(Color.White),
//            .height(56.dp)
        containerColor = Color.White,
        contentColor = Color.White,
        indicator = {},
        divider = {},
        tabs = {
            for (index in 0..(menuList.size - 1)) {
                var item = menuList.get(index)
                Tab(
                    selected = selecctTab == index,
                    onClick = {
                        KLog.i("znh","button_item_click"+index)
                        selecctTab = index
//                        itemClick(selecctTab)
                        item.action()
                    },
                    text = { itemText(item.name, selecctTab == index) },
                    icon = { itemIcon(item.icon, item.iconSel, selecctTab == index) }
                )
            }
        },
    )

}


public class BottonController{
    var selecctTab: MutableState<Int> = mutableStateOf(0)
    fun getCut():MutableState<Int>{
       return selecctTab
    }
    fun selectItem(index:Int){
        selecctTab.value=index
    }

}

@Composable
fun itemText(msg: String, isSelect: Boolean = false) {
    Text(text = msg,
        color = if (isSelect) AppColor.TxtSelColor.color else AppColor.TxtCom.color,
        fontSize = 14.sp)
}

@Composable
fun itemIcon(msg: String, msg2: String, isSelect: Boolean = false) {
    var imgW =
        if (isSelect) {
            40.dp
        } else {
            20.dp
        }
    Image(
        painter = rememberAsyncImagePainter(ImageUri.SCHEME_COMMON_ASSETS + if (isSelect) msg2 else msg),
        contentDescription = "图标",
        contentScale = ContentScale.FillWidth,
//        colorFilter = ColorFilter.tint(
//            Color(255,0,0,125),
//            blendMode = BlendMode.Overlay
//        ),
//        colorFilter = ColorFilter.tint(Color.Green),
        modifier = Modifier
            .width(
                imgW
            )
            .clip(RoundedCornerShape(5.dp))
            .height(
                imgW
            )
            .backgroundColor(if (isSelect) AppColor.BgSelColor.color else Color.Transparent)
    )
}



